Ngundenng Analytics Technologies LLC - Analysis of the USA-Mexican Auto Imports

Ngundenng Analytics Technologies LLC - Analysis of the USA-Mexican Auto Imports

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(fpp3)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.2 ──
✔ tsibble     1.1.6     ✔ feasts      0.4.2
✔ tsibbledata 0.4.1     ✔ fable       0.4.1
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()
#Warning: x = 'NGDPRNSAXDCSSD', get = 'economic.data': Error in getSymbols.FRED(Symbols = "NGDPRNSAXDCSSD", env = <environment>, : Unable to import “NGDPRNSAXDCSSD”.
#cannot open the connection
imports <- tidyquant::tq_get(
  x = "MAUINSA",
  get = "economic.data",
  from = "1993-01-01",
  to = "2025-10-01"
  
  
) |> 
  mutate(date = yearmonth(date) #
  ) |> as_tsibble(
    index = date)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
imports
# A tsibble: 394 x 3 [1M]
   symbol      date price
   <chr>      <mth> <dbl>
 1 MAUINSA 1993 Jan   7.5
 2 MAUINSA 1993 Feb  18.9
 3 MAUINSA 1993 Mar  10.7
 4 MAUINSA 1993 Apr  19.9
 5 MAUINSA 1993 May  19.7
 6 MAUINSA 1993 Jun  25.7
 7 MAUINSA 1993 Jul  15  
 8 MAUINSA 1993 Aug  13.1
 9 MAUINSA 1993 Sep  24.6
10 MAUINSA 1993 Oct  23.9
# ℹ 384 more rows
1. Se selecciona `get = "economic.data"` para obtener datos económicos de la base de datos FRED (Federal Reserve Economic Data).
2. Se convierte la columna `date` a un formato de fecha utilizando `yearmonth()` de la librería `lubridate`, ....

## Instrucciones 

Realizar un pronostico a 14 meses (hasta diciembre de 2025). Realizar el flujo de pronóstico completo
h = 14 
imports_train <- imports |> 
  slice(1:(n() - h))

imports_train
# A tsibble: 380 x 3 [1M]
   symbol      date price
   <chr>      <mth> <dbl>
 1 MAUINSA 1993 Jan   7.5
 2 MAUINSA 1993 Feb  18.9
 3 MAUINSA 1993 Mar  10.7
 4 MAUINSA 1993 Apr  19.9
 5 MAUINSA 1993 May  19.7
 6 MAUINSA 1993 Jun  25.7
 7 MAUINSA 1993 Jul  15  
 8 MAUINSA 1993 Aug  13.1
 9 MAUINSA 1993 Sep  24.6
10 MAUINSA 1993 Oct  23.9
# ℹ 370 more rows
imports_train |> 
  autoplot(price) +
  labs(
    title = "Time series plot of the Auto Imports of Mexico",
    y = "Imports"
  )

library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(fpp3)
imports_train |> 
  gg_season(price) |> 
  ggplotly()
Warning: `gg_season()` was deprecated in feasts 0.4.2.
ℹ Please use `ggtime::gg_season()` instead.
DecJanFebMarAprMayJunJulAugSepOctNov50100
200220122022dateprice
imports_train |> 
  autoplot(log(price))

price_lambda <- imports_train |> 
  features(price, guerrero) |> 
  pull()
price_lambda
[1] 0.1414644
model_fit <- imports_train |> 
  model(
    media = MEAN(box_cox(price, price_lambda)),
    naive = NAIVE(box_cox(price, price_lambda)),
    snaive = SNAIVE(box_cox(price, price_lambda)),
    drift = RW(box_cox(price, price_lambda) ~ drift())
  )

model_fit
# A mable: 1 x 4
    media   naive   snaive         drift
  <model> <model>  <model>       <model>
1  <MEAN> <NAIVE> <SNAIVE> <RW w/ drift>
imports_train |> 
  autoplot(box_cox(price, price_lambda))

imports_train |> 
  model(
    stl = STL(box_cox(price, price_lambda), robust = TRUE)
  ) |> 
    components() |> 
  autoplot()

imports_fit <- imports_train |> 
  model(
    media = MEAN(box_cox(price, price_lambda)),
    naive = NAIVE(box_cox(price, price_lambda)),
    snaive = SNAIVE(box_cox(price, price_lambda)),
    drift = RW(box_cox(price, price_lambda) ~ drift())
  )

imports_fit
# A mable: 1 x 4
    media   naive   snaive         drift
  <model> <model>  <model>       <model>
1  <MEAN> <NAIVE> <SNAIVE> <RW w/ drift>
imports_aug <- imports_fit |> 
  augment()


imports_fit |>
  select(media) |>
  gg_tsresiduals() +
  labs(
    title = "Residuos del modelo media"
  )
Warning: `gg_tsresiduals()` was deprecated in feasts 0.4.2.
ℹ Please use `ggtime::gg_tsresiduals()` instead.

imports_aug |> 
  ggplot(aes(x = date, y = .innov)) + 
  geom_line() +
  facet_wrap(vars(.model), scales = "free_y") + 
  labs(
    title = "Innovaciones de los modelos benchmark"
  )
Warning: Removed 14 rows containing missing values or values outside the scale range
(`geom_line()`).

imports_fit_stl <- imports_train |> 
  model(
    stlf = decomposition_model(
      STL(box_cox(price, price_lambda), robust = TRUE),
      SNAIVE(season_year),
      RW(season_adjust ~ drift())
    )
  )

imports_fit_stl
# A mable: 1 x 1
                       stlf
                    <model>
1 <STL decomposition model>
imports_fit_stl |> 
  gg_tsresiduals()
Warning: Removed 12 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 12 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 12 rows containing non-finite outside the scale range
(`stat_bin()`).
Warning: Removed 12 rows containing missing values or values outside the scale range
(`geom_rug()`).

imports_fit_todos <- imports_train |> 
  model(
    media = MEAN(box_cox(price, price_lambda)),
    naive = NAIVE(box_cox(price, price_lambda)),
    snaive = SNAIVE(box_cox(price, price_lambda)),
    drift = RW(box_cox(price, price_lambda) ~ drift()),
    stlf = decomposition_model(
      STL(box_cox(price, price_lambda), robust = TRUE),
      SNAIVE(season_year),
      RW(season_adjust ~ drift())
    )
  )
imports_fit_todos
# A mable: 1 x 5
    media   naive   snaive         drift                      stlf
  <model> <model>  <model>       <model>                   <model>
1  <MEAN> <NAIVE> <SNAIVE> <RW w/ drift> <STL decomposition model>
imports_fc_todos <- imports_fit_todos |> 
  forecast(h = "5 years")

imports_fc_todos
# A fable: 300 x 4 [1M]
# Key:     .model [5]
   .model     date           price .mean
   <chr>     <mth>          <dist> <dbl>
 1 media  2024 Sep t(N(5.7, 0.84))  72.5
 2 media  2024 Oct t(N(5.7, 0.84))  72.5
 3 media  2024 Nov t(N(5.7, 0.84))  72.5
 4 media  2024 Dec t(N(5.7, 0.84))  72.5
 5 media  2025 Jan t(N(5.7, 0.84))  72.5
 6 media  2025 Feb t(N(5.7, 0.84))  72.5
 7 media  2025 Mar t(N(5.7, 0.84))  72.5
 8 media  2025 Apr t(N(5.7, 0.84))  72.5
 9 media  2025 May t(N(5.7, 0.84))  72.5
10 media  2025 Jun t(N(5.7, 0.84))  72.5
# ℹ 290 more rows
imports_fc_todos |> 
  autoplot(imports) +
  facet_wrap(vars(.model), scales = "free_y")

imports_fc_todos |> 
  autoplot(imports, level = NULL, linewidth = 1)

imports_fc_todos |> 
  accuracy(imports) |> 
  arrange(RMSE)
Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing. 
46 observations are missing between 2025 Nov and 2029 Aug
# A tibble: 5 × 10
  .model .type    ME  RMSE   MAE   MPE  MAPE  MASE RMSSE    ACF1
  <chr>  <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
1 drift  Test   9.35  16.3  13.5  7.24  13.1  1.02 0.902 -0.227 
2 naive  Test  13.4   19.1  16.8 11.0   15.7  1.26 1.06  -0.204 
3 stlf   Test  12.6   20.3  15.8 10.1   14.6  1.19 1.12   0.0756
4 snaive Test  18.7   26.7  22.5 15.2   20.1  1.70 1.48   0.162 
5 media  Test  32.1   36.8  32.4 28.4   28.8  2.44 2.03   0.188 
imports |> 
  model(
    drift = RW(box_cox(price, price_lambda))
  ) |> 
    forecast(h = "3 years") |>
    autoplot(imports)